package io.xccit.service.impl;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.xccit.pojo.param.ProductSearchParam;
import io.xccit.pojo.product.Product;
import io.xccit.result.AjaxResult;
import io.xccit.service.ISearchService;
import lombok.extern.slf4j.Slf4j;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author CH_ywx
 * @date 2023-09-13
 * @description
 */
@Service
@Slf4j
public class SearchServiceImpl implements ISearchService {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    /**
     * 根据关键字查询商品
     *
     * @param productSearchParam
     * @return
     */
    @Override
    public AjaxResult searchProduct(ProductSearchParam productSearchParam) {
        SearchRequest searchRequest = new SearchRequest("product");
        String search = productSearchParam.getSearch();
        if (StringUtils.isEmpty(search)){
            // 参数为空,查询所有
            searchRequest.source().query(QueryBuilders.matchAllQuery());
        }else {
            // 参数不为空,根据条件查询
            searchRequest.source().query(QueryBuilders.matchQuery("all",search));
        }
        //分页查询
        searchRequest.source().from((productSearchParam.getCurrentPage() -1)*productSearchParam.getPageSize());
        searchRequest.source().size(productSearchParam.getPageSize());
        SearchResponse searchResponse = null;
        try {
            searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new RuntimeException("查询失败");
        }
        //数据
        SearchHits hits = searchResponse.getHits();
        List<Product> productList = new ArrayList<>();
        long total = hits.getTotalHits().value;
        //数据集合
        SearchHit[] searchHits = hits.getHits();
        ObjectMapper objectMapper = new ObjectMapper();
        for (SearchHit searchHit : searchHits) {
            //查询的商品数据
            String sourceAsString = searchHit.getSourceAsString();
            Product product = null;
            try {
                //TODO 由于ProductDoc中一些属性是Product没有的,所以Product中添加了JsonIgnoreProperties注解
                product = objectMapper.readValue(sourceAsString, Product.class);
                log.warn(product.toString());
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            productList.add(product);
        }
        AjaxResult ajaxResult = AjaxResult.success(productList, total);
        return ajaxResult;
    }
}
